package com.xy.xyaicpzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.xyaicpzs.domain.entity.PredictRecord;
import com.xy.xyaicpzs.mapper.PredictRecordMapper;
import com.xy.xyaicpzs.service.PredictRecordService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author XY003
* @description 针对表【predict_record(彩票开奖信息表)】的数据库操作Service实现
* @createDate 2025-06-16 13:17:53
*/
@Service
public class PredictRecordServiceImpl extends ServiceImpl<PredictRecordMapper, PredictRecord>
    implements PredictRecordService{

    @Override
    public PredictRecord createPredictRecord(Long userId, Long drawId, Date drawDate, List<Integer> redBalls, Integer blueBall) {
        PredictRecord predictRecord = new PredictRecord();
        predictRecord.setUserId(userId);
        predictRecord.setDrawId(drawId);
        predictRecord.setDrawDate(drawDate);
        predictRecord.setPredictTime(new Date());
        
        // 设置红球号码
        if (redBalls != null && redBalls.size() >= 6) {
            predictRecord.setRedBall1(redBalls.get(0));
            predictRecord.setRedBall2(redBalls.get(1));
            predictRecord.setRedBall3(redBalls.get(2));
            predictRecord.setRedBall4(redBalls.get(3));
            predictRecord.setRedBall5(redBalls.get(4));
            predictRecord.setRedBall6(redBalls.get(5));
        }
        
        // 设置蓝球号码
        if (blueBall != null) {
            predictRecord.setBlueBall(blueBall);
        }
        
        // 默认状态为待开奖
        predictRecord.setPredictStatus("待开奖");
        predictRecord.setType("ssq");
        
        save(predictRecord);
        return predictRecord;
    }

    @Override
    public List<PredictRecord> getPredictRecordsByUserId(Long userId) {
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).orderByDesc("predictTime");
        return list(queryWrapper);
    }

    @Override
    public List<PredictRecord> getPredictRecordsByUserIdWithPaging(Long userId, Integer page, Integer size) {
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).orderByDesc("predictTime");
        
        // 计算偏移量
        int offset = (page - 1) * size;
        queryWrapper.last("LIMIT " + offset + ", " + size);
        
        return list(queryWrapper);
    }

    @Override
    public Long getPredictRecordsCountByUserId(Long userId) {
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        return count(queryWrapper);
    }
} 